package com.scoy.honeymei.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.HttpParams;
import com.scoy.honeymei.R;
import com.scoy.honeymei.adapter.CouponListAdapter0;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.bean.CouponBean0;
import com.scoy.honeymei.http.HpCallback;
import com.scoy.honeymei.http.HpGo;
import com.scoy.honeymei.url.ApiCallBack;
import com.scoy.honeymei.url.MeConstant;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;
import com.scoy.honeymei.utils.SPHelper;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: CouponActivity$
 * @Description: 优惠券界面
 * @Author: GaoZhan
 * @CreateDate: 2019/9/2/0002$ 下午 14:26$
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/9/2/0002$ 下午 14:26$
 * @UpdateRemark: 更新内容
 */
public class CouponActivity extends BaseActivity {


    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.normal_rv)
    RecyclerView normalRv;
    @BindView(R.id.nodata_tv)
    TextView nodataTv;
    @BindView(R.id.nor_srl)
    SmartRefreshLayout norSrl;
    private CouponActivity mContext;
    private ArrayList<CouponBean0> datalist;
    private CouponListAdapter0 couponListAdapter;
    private int pageInt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_activity_list15);
        ButterKnife.bind(this);
        mContext = CouponActivity.this;

        initNormal();

        httpTravelList(0);

    }

    @Override
    public void initNormal() {
        titleTv.setText("优惠券");

        MyUtil.setStatusBar(mContext, getWindow(), true, R.color.colorWhite);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

        datalist = new ArrayList<>();
        initRV();
        norSrl.setOnRefreshListener(refreshLayout -> {
            refreshLayout.finishRefresh(1500);
            datalist.clear();
            httpTravelList(0);

        }).setOnLoadMoreListener(refreshLayout -> {
            refreshLayout.finishLoadMore(1500);
            httpTravelList(pageInt);

        });
    }

    private void initRV() {
        couponListAdapter = new CouponListAdapter0(mContext, datalist, nodataTv);
        normalRv.setLayoutManager(new LinearLayoutManager(mContext, RecyclerView.VERTICAL, false));
        normalRv.setAdapter(couponListAdapter);
        couponListAdapter.setOnOneClick(pos -> {
            CouponBean0 couponBean = datalist.get(pos);
            httpGot(couponBean.getId());
        });

    }

    @OnClick({R.id.back_iv, R.id.title_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
            case R.id.title_tv:
                break;
        }
    }

    private void httpTravelList(int myPage) {
        HttpParams params = new HttpParams();
        params.put("page", myPage);
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.COUPON_LIST, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                List<CouponBean0> data = new Gson().fromJson(o, new TypeToken<List<CouponBean0>>() {
                }.getType());
                pageInt = myPage + 1;
                datalist.addAll(data);
                couponListAdapter.notifyDataSetChanged();
            }

        });

    }

    //领取
    private void httpGot(int cId) {
        HttpParams params = new HttpParams();
        params.put("coupon_id", cId);
        params.put("uid", SPHelper.getString(MeConstant.UID, ""));
        HpGo.newInstance().HttpGo(this, MyUrl.COUPON_GET, params, new ApiCallBack() {
            @Override
            public void onSuccess(String o) {
                norSrl.autoRefresh();
                MyUtil.mytoast0(mContext, "已成功领取", R.mipmap.ic_sure);
            }
        });

    }
}
